
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Friendster.Service.Core;
using System.IO;
using System.Web;

namespace Friendster.Service
{
    public sealed class NotificationsService
    {
        private string _session;

        internal NotificationsService(string session)
        {
            this._session = session;
        }
        public ICollection<string> SendNotification(ICollection<string> userIDs, string subject, string content, string label, string urlFragment)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);

            if ((userIDs == null) || (userIDs.Count == 0))
            {
                throw new ArgumentNullException("userIDs");
            }
            if (string.IsNullOrEmpty(subject))
            {
                throw new ArgumentNullException("subject");
            }
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("content");
            }
            if (string.IsNullOrEmpty(label))
            {
                throw new ArgumentNullException("label");
            }
            if (string.IsNullOrEmpty(urlFragment))
            {
                throw new ArgumentNullException("urlFragment");
            }

            StringBuilder builder = new StringBuilder();
            foreach (string str in userIDs)
            {
                if (builder.Length != 0)
                {
                    builder.Append(",");
                }
                builder.Append(str);
            }
            
            request.Parameters.Add("uids",builder.ToString());
            request.Parameters.Add("content",content);
            request.Parameters.Add("subject",subject);
            request.Parameters.Add("label",label);
            request.Parameters.Add("url_fragment",urlFragment);
            request.Parameters.Add("type","2");

            FriendsterResponse response = request.InvokeMethod("POST", "notification/" + builder.ToString());

            if (!response.IsError)
            {
                try
                {
                    ArrayList Result = (ArrayList)response.Result;
                    string[] uids = new string[Result.Count];
                    for (int i = 0; i < Result.Count; i++)
                    {
                        Hashtable res = (Hashtable)Result[i];
                        uids[i] = res["uid"].ToString();
                    }
                    return uids;
                }
                catch(Exception e)
                {
                    return null;
                }
            }
            return null;
        }
        
        public string Send(string userID, string subject, string content, string label, string urlFragment)
        {
            if (string.IsNullOrEmpty(userID))
            {
                throw new ArgumentNullException("userID");
            }

            ICollection<string> notif = this.SendNotification(new string[] { userID }, subject, content, label, urlFragment);
            if ((notif != null) && (notif.Count != 0))
            {
                foreach (string str in notif)
                {
                    return str;
                }
            }
            return null;
        }

        public string Send(string subject, string content, string label, string urlFragment)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            return this.Send(request.UserID, subject, content, label, urlFragment);
        }

    }
}
